D:\a\tools.proto\tools.proto\compiler\src\gen\rust\message\write.rs
Line | Count | Source |
1 | | // Copyright (c) 2024, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use crate::compiler::message::Message; |
30 | | use crate::compiler::util::types::TypePathMap; |
31 | | use crate::gen::base::map::{DefaultTypeMapper, TypePathMapper}; |
32 | | use crate::gen::base::message::Templates; |
33 | | use crate::gen::base::message_write::generate; |
34 | | use crate::gen::base::Error; |
35 | | use crate::gen::rust::util::{gen_where_clause, Lifetime, RustUtils}; |
36 | | use crate::gen::template::Template; |
37 | | use crate::gen::{codec::CodecMap, RustParams}; |
38 | | use itertools::Itertools; |
39 | | |
40 | | const TEMPLATE: &[u8] = include_bytes!("write.template"); |
41 | | |
42 | 45 | fn _gen_message_write_impl( |
43 | 45 | msg: &Message, |
44 | 45 | codec_map: &CodecMap, |
45 | 45 | type_path_map: &TypePathMapper<DefaultTypeMapper>, |
46 | 45 | generics: &str, |
47 | 45 | impl_generics: &str, |
48 | 45 | function: &str, |
49 | 45 | ) -> Result<String, Error> { |
50 | 45 | let mut templates = Templates { |
51 | 45 | template: Template::compile(TEMPLATE).unwrap(), |
52 | 45 | codec_map, |
53 | 45 | }; |
54 | 45 | let where_clauses = msg |
55 | 45 | .fields |
56 | 45 | .iter() |
57 | 91 | .map(|field| gen_where_clause(&templates.template, field, type_path_map, function)) |
58 | 45 | .join(""); |
59 | 45 | templates.template.var("where_clauses", where_clauses); |
60 | 45 | if generics.is_empty() { Branch (60:8): [True: 35, False: 10]
Branch (60:8): [Folded - Ignored]
|
61 | 35 | templates.template.var("impl_generics", "").var("generics", "<'_>"); |
62 | 35 | } else { |
63 | 10 | templates.template.var("generics", generics).var("impl_generics", impl_generics); |
64 | 10 | } |
65 | 45 | generate::<RustUtils, _>(templates, msg, type_path_map, function) |
66 | 45 | } |
67 | | |
68 | 31 | pub fn gen_message_write_impl( |
69 | 31 | msg: &Message, |
70 | 31 | codec_map: &CodecMap, |
71 | 31 | type_path_map: &TypePathMap, |
72 | 31 | params: &RustParams, |
73 | 31 | ) -> Result<String, Error> { |
74 | 31 | let type_path_map = TypePathMapper::new(type_path_map, DefaultTypeMapper); |
75 | 31 | let generics = RustUtils::get_generics_for_write(msg, &type_path_map, Lifetime::Named).into_string(); |
76 | 31 | let mut code = _gen_message_write_impl(msg, codec_map, &type_path_map, &generics, &generics, "impl")?0 ; |
77 | 55 | if msg.fields.iter().any(31 |v| v.header.is_some())31 { Branch (77:8): [True: 6, False: 25]
Branch (77:8): [Folded - Ignored]
|
78 | 6 | let shape_generics = RustUtils::get_generics_for_write(msg, &type_path_map, Lifetime::Anonymous).into_string(); |
79 | 6 | let impl_shape_generics = RustUtils::get_generics_for_write(msg, &type_path_map, Lifetime::None).into_string(); |
80 | 6 | code += &_gen_message_write_impl(msg, codec_map, &type_path_map, &shape_generics, &impl_shape_generics, "impl_shape_write")?0 ; |
81 | 25 | } |
82 | 31 | if params.enable_write_async { Branch (82:8): [True: 6, False: 25]
Branch (82:8): [Folded - Ignored]
|
83 | 6 | code += &_gen_message_write_impl(msg, codec_map, &type_path_map, &generics, &generics, "impl_async")?0 ; |
84 | 11 | if msg.fields.iter().any(6 |v| v.header.is_some())6 { Branch (84:12): [True: 2, False: 4]
Branch (84:12): [Folded - Ignored]
|
85 | 2 | code += &_gen_message_write_impl(msg, codec_map, &type_path_map, &generics, &generics, "impl_shape_write_async")?0 ; |
86 | 4 | } |
87 | 25 | } |
88 | 31 | Ok(code) |
89 | 31 | } |